﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects;
using BusinessObjects.BO_GetObjects;
using LibraryO2S;

public partial class controls_admin_AdminUserAdd : Pages_Base
{
    private DataTable objDT;
    private string exceptRole = string.Empty;
    private string plusRole = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            loadSetting();
            loadListBoxGroup(lsbParent, 0);
            lsbParent.SelectedIndex = 0;
            lsbParent_SelectedIndexChanged(null, null);
        }
    }
    private void getExceptRole(int admin_id)
    {
        exceptRole = exceptRole + admin_id.ToString() + ";";
        DataTable c_RoleDT = BO_Sec_Admin_Modules.SelectParent(1, admin_id);
        if (c_RoleDT != null && c_RoleDT.Rows.Count > 0)
        {
            foreach (DataRow c_dr in c_RoleDT.Rows)
            {
                getExceptRole(Convert.ToInt32(c_dr["ADMIN_MODULE_ID"]));
            }
        }
    }
    private void getPlusRole(int admin_id)
    {
        plusRole = plusRole + admin_id.ToString() + ";";
        DataTable c_RoleDT = BO_Sec_Admin_Modules.SelectParent(1, admin_id);
        if (c_RoleDT != null && c_RoleDT.Rows.Count > 0)
        {
            foreach (DataRow c_dr in c_RoleDT.Rows)
            {
                getPlusRole(Convert.ToInt32(c_dr["ADMIN_MODULE_ID"]));
            }
        }
    }
    protected void btnNewUser_Click(object sender, EventArgs e)
    {
        try
        {
            base.OpenConnection();
            string parent = string.Empty;
            foreach (ListItem item in lsbParent.Items)
            {
                if (item.Selected)
                    parent = parent + item.Value + ";";
            }
            foreach (ListItem item in lsbExceptRole.Items)
            {
                if (item.Selected)
                    getExceptRole(Convert.ToInt32(item.Value));
            }
            foreach (ListItem item in lsbPlusRole.Items)
            {
                if (item.Selected)
                    getPlusRole(Convert.ToInt32(item.Value));
            }
            DataTable objDT = (DataTable)Session["admin"];
            string strReturn = BO_Sec_Administrators.Exist(txtUsername.Text.Trim());
            if (strReturn.Equals("-1"))
            {
                divMessage.InnerHtml = O2S_Message.Error("Thông báo", "Tài khoản bạn chọn đã có người sử dụng!");
                return;
            }
            if (strReturn.Equals("0"))
            {
                string strResult = BO_Sec_Administrators.Insert(parent, plusRole, exceptRole, txtFullName.Text, txtUsername.Text, O2S_Encrypt.ToMD5(txtPassword.Text), txtPhone.Text, txtAddress.Text, txtEmail.Text, DateTime.Now, 0, DateTime.Now, 1, DateTime.Now, /*objDT.Rows[0]["user_name"].ToString()*/"ngocnh", DateTime.Now, /*objDT.Rows[0]["user_name"].ToString()*/"ngocnh", true);
                if (strResult != "0")
                {
                    divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Đã Thêm Quản Trị Viên Mới!");
                }
                else
                {
                    divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có Lỗi Trong quá trình xử lý!");
                }
            }
            else
            {
                DataTable objDTA = BO_Sec_Administrators.Search(Convert.ToInt32(strReturn));
                string strResult = BO_Sec_Administrators.Update(Convert.ToInt32(objDTA.Rows[0]["admin_id"].ToString()), parent, plusRole, exceptRole, txtFullName.Text, txtUsername.Text, O2S_Encrypt.ToMD5(txtPassword.Text), txtPhone.Text, txtAddress.Text, txtEmail.Text, DateTime.Now, 0, DateTime.Now, 1, DateTime.Now, "ngocnh", DateTime.Now, /*objDT.Rows[0]["user_name"].ToString()*/"ngocnh", true);
                if (strResult != "0")
                {
                    divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Đã Thêm Quản Trị Viên Mới!");
                }
                else
                {
                    divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có Lỗi Trong quá trình xử âlý!");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    protected void loadListBoxGroup(ListBox lsb, int parent)
    {
        try
        {
            objDT = BO_Sec_Group_Admin.SelectParent(1, parent);
            if (objDT != null && objDT.Rows.Count > 0)
            {
                foreach (DataRow dr in objDT.Rows)
                {
                    lsb.Items.Add(new ListItem(dr["GROUP_NAME"].ToString(), dr["GROUP_ADMIN_ID"].ToString()));
                    loadListBoxChildGroup(lsb, Convert.ToInt32(dr["GROUP_ADMIN_ID"].ToString()), "");
                }
            }
            else lsb.Items.Add(new ListItem("---Chưa có nhóm nào---", "0"));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void loadListBoxChildGroup(ListBox lsbChild, int parentid, string identify)
    {
        try
        {
            DataTable objDTChild = BO_Sec_Group_Admin.SelectParent(1, parentid);
            if (objDTChild != null && objDTChild.Rows.Count > 0)
            {
                identify += "--- ";
                foreach (DataRow drChild in objDTChild.Rows)
                {
                    lsbChild.Items.Add(new ListItem(identify + drChild["GROUP_NAME"].ToString(), drChild["GROUP_ADMIN_ID"].ToString()));
                    loadListBoxChildGroup(lsbChild, Convert.ToInt32(drChild["GROUP_ADMIN_ID"].ToString()), identify);
                }
            }
        }
        catch (Exception ex) { throw ex; }
    }
    private void loadSetting()
    {
        objDT = BO_Settings.SelectByGroupId(6, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                //case "txtNoteGroup":
                //    txtNoteGroup.InnerText = value;
                //    break;
                //case "txtNoteFullname":
                //    txtNoteFullname.InnerText = value;
                //    break;
                //case "txtNoteUserName":
                //    txtNoteUserName.InnerText = value;
                //    break;
                //case "txtNotePass":
                //    txtNotePass.InnerText = value;
                //    break;
                //case "txtNoteRePass":
                //    txtNoteRePass.InnerText = value;
                //    break;
                //case "txtNotePhone":
                //    txtNotePhone.InnerText = value;
                //    break;
                //case "txtNoteAddress":
                //    txtNoteAddress.InnerText = value;
                //    break;
                //case "txtNoteEmail":
                //    txtNoteEmail.InnerText = value;
                //    break;
                //case "txtNotePlusRole":
                //    txtNotePlusRole.InnerText = value;
                //    break;
                //case "txtNoteExceptRole":
                //    txtNoteExceptRole.InnerText = value;
                //    break;
            }
        }
    }
    private void bindLsbPlusRole()
    {
        lsbPlusRole.Items.Clear();
        string list_group = string.Empty;
        foreach (ListItem item in lsbParent.Items)
        {
            if (item.Selected)
                list_group = list_group + item.Value + ";";
        }
        string list_admin_module = ";" + BO_Sec_Administrators.GetPlusRole(list_group);
        base.bindAdminModules(lsbPlusRole, 0, list_admin_module);
    }
    private void bindLsbExceptRole()
    {
        lsbExceptRole.Items.Clear();
        string list_group = string.Empty;
        foreach (ListItem item in lsbParent.Items)
        {
            if (item.Selected)
                list_group = list_group + item.Value + ";";
        }
        string list_admin_module = ";" + BO_Sec_Administrators.GetPlusRole(list_group);
        base.bindAdminModules(lsbExceptRole, 0, list_admin_module);
    }
    protected void lsbParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindLsbExceptRole();
        bindLsbPlusRole();
    }
}
